library(Rgb)
library(foreign)
library(sandwich)
library(lmtest)
library(plyr)
library(apsrtable)
library(xtable)
library(car)

covariates <- read.csv('survey2_updateddemos.csv')

#Pull Out Amount Covariates Not Already in Long File
divisive.info <- covariates[,c('rnid','divisive.rounds.round1porder','divisive.rounds.round2porder','divisive.rounds.round3porder','divisive.rounds.round4porder','divisive.rounds.round5porder','divisive.rounds.round6porder','divisive.rounds.round7porder')]
names(divisive.info) <- c('rnid','round1.party','round2.party','round3.party','round4.party','round5.party','round6.party','round7.party')
divisive.info$round1.party <- as.numeric(as.character(divisive.info$round1.party))
divisive.info$round2.party <- as.numeric(as.character(divisive.info$round2.party))
divisive.info$round3.party <- as.numeric(as.character(divisive.info$round3.party))
divisive.info$round4.party <- as.numeric(as.character(divisive.info$round4.party))
divisive.info$round5.party <- as.numeric(as.character(divisive.info$round5.party))
divisive.info$round6.party <- as.numeric(as.character(divisive.info$round6.party))
divisive.info$round7.party <- as.numeric(as.character(divisive.info$round7.party))
divisive.info <- subset(divisive.info,divisive.info$round1.party!='')

divisive.ids <- as.character(divisive.info$rnid)
task.num <- c(1,2,3,4,5,6,7)
candidate.num <- c(1,2)

divisive.merge <- expand.grid(divisive.ids,task.num,candidate.num)
names(divisive.merge) <- c('rnid','task','profile')

divisive.merge <- merge(divisive.merge,divisive.info,by=c('rnid'),all.x=TRUE)
divisive.merge$candidate.party <- NA
divisive.merge$candidate.party[which(divisive.merge$task==1)] <- divisive.merge$round1.party[which(divisive.merge$task==1)]
divisive.merge$candidate.party[which(divisive.merge$task==2)] <- divisive.merge$round2.party[which(divisive.merge$task==2)]
divisive.merge$candidate.party[which(divisive.merge$task==3)] <- divisive.merge$round3.party[which(divisive.merge$task==3)]
divisive.merge$candidate.party[which(divisive.merge$task==4)] <- divisive.merge$round4.party[which(divisive.merge$task==4)]
divisive.merge$candidate.party[which(divisive.merge$task==5)] <- divisive.merge$round5.party[which(divisive.merge$task==5)]
divisive.merge$candidate.party[which(divisive.merge$task==6)] <- divisive.merge$round6.party[which(divisive.merge$task==6)]
divisive.merge$candidate.party[which(divisive.merge$task==7)] <- divisive.merge$round7.party[which(divisive.merge$task==7)]

divisive.merge$round1.party <- NULL
divisive.merge$round2.party <- NULL
divisive.merge$round3.party <- NULL
divisive.merge$round4.party <- NULL
divisive.merge$round5.party <- NULL
divisive.merge$round6.party <- NULL
divisive.merge$round7.party <- NULL

divisive.merge$candidate.party[which(divisive.merge$profile==1 & divisive.merge$candidate.party==0)] <- 'Republican'
divisive.merge$candidate.party[which(divisive.merge$profile==1 & divisive.merge$candidate.party==1)] <- 'Democrat'

divisive.merge$candidate.party[which(divisive.merge$profile==2 & divisive.merge$candidate.party==0)] <- 'Democrat'
divisive.merge$candidate.party[which(divisive.merge$profile==2 & divisive.merge$candidate.party==1)] <- 'Republican'

divisive.conjoint <- read.csv('conjoint_divisive.csv')
divisive.conjoint <- merge(divisive.conjoint,divisive.merge,by.x=c('rnid','task','profile'),by.y=c('rnid','task','profile'),all.x=TRUE)

#Determine Candidate Positions
divisive.conjoint$abortion.candidate <- as.numeric(as.character(mapvalues(x=divisive.conjoint$Abortion,from=c('Make abortions illegal','Do not make abortions illegal'),to=c(1,0))))
divisive.conjoint$rifle.candidate <- as.numeric(as.character(mapvalues(x=as.character(divisive.conjoint$Assault.Rifle.Ban),from=c('Ban assault rifles ','Do not ban assault rifles'),to=c(1,0))))
divisive.conjoint$obamacare.candidate <- as.numeric(as.character(mapvalues(x=divisive.conjoint$Obamacare,from=c('Supports Obamacare repeal','Opposes Obamacare repeal'),to=c(1,0))))
divisive.conjoint$lgbt.candidate <- as.numeric(as.character(mapvalues(x=divisive.conjoint$LGBT.Protection,from=c('Supports laws protecting LGBT people against discrimination','Opposes laws protecting LGBT people against discrimination'),to=c(1,0))))
divisive.conjoint$birthcontrol.candidate <- as.numeric(as.character(mapvalues(x=divisive.conjoint$Health.Insurance,from=c('Require insurance to cover birth control','Do not require insurance to cover birth conrol'),to=c(1,0)))) 
divisive.conjoint$china.candidate <- as.numeric(as.character(mapvalues(x=divisive.conjoint$Trade.Policy,from=c('Place a tariff on imported goods from China','Do not place a tariff on imported goods from China'),to=c(1,0))))
divisive.conjoint$interior.candidate <- as.numeric(as.character(mapvalues(x=divisive.conjoint$Interior.Department,from=c('Supports spending increase at the Department of Interior','Opposes spending increase at the Department of the Interior'),to=c(1,0))))
divisive.conjoint$commoncore.candidate <- as.numeric(as.character(mapvalues(x=divisive.conjoint$Common.Core,from=c('Supports Common Core standards','Opposes Common Core standards'),to=c(1,0))))

#Determine Reputation-Consistent of Candidate Positions
divisive.conjoint$abortion.reputation.consistent <- NA
divisive.conjoint$abortion.reputation.consistent[which(divisive.conjoint$candidate.party=='Republican' & divisive.conjoint$abortion.candidate==1)] <- 1
divisive.conjoint$abortion.reputation.consistent[which(divisive.conjoint$candidate.party=='Democrat' & divisive.conjoint$abortion.candidate==0)] <- 1
divisive.conjoint$abortion.reputation.consistent[which(divisive.conjoint$candidate.party=='Republican' & divisive.conjoint$abortion.candidate==0)] <- 0
divisive.conjoint$abortion.reputation.consistent[which(divisive.conjoint$candidate.party=='Democrat' & divisive.conjoint$abortion.candidate==1)] <- 0

divisive.conjoint$rifle.reputation.consistent <- NA
divisive.conjoint$rifle.reputation.consistent[which(divisive.conjoint$candidate.party=='Republican' & divisive.conjoint$rifle.candidate==1)] <- 0
divisive.conjoint$rifle.reputation.consistent[which(divisive.conjoint$candidate.party=='Democrat' & divisive.conjoint$rifle.candidate==0)] <- 0
divisive.conjoint$rifle.reputation.consistent[which(divisive.conjoint$candidate.party=='Republican' & divisive.conjoint$rifle.candidate==0)] <- 1
divisive.conjoint$rifle.reputation.consistent[which(divisive.conjoint$candidate.party=='Democrat' & divisive.conjoint$rifle.candidate==1)] <- 1

divisive.conjoint$obamacare.reputation.consistent <- NA
divisive.conjoint$obamacare.reputation.consistent[which(divisive.conjoint$candidate.party=='Republican' & divisive.conjoint$obamacare.candidate==1)] <- 1
divisive.conjoint$obamacare.reputation.consistent[which(divisive.conjoint$candidate.party=='Democrat' & divisive.conjoint$obamacare.candidate==0)] <- 1
divisive.conjoint$obamacare.reputation.consistent[which(divisive.conjoint$candidate.party=='Republican' & divisive.conjoint$obamacare.candidate==0)] <- 0
divisive.conjoint$obamacare.reputation.consistent[which(divisive.conjoint$candidate.party=='Democrat' & divisive.conjoint$obamacare.candidate==1)] <- 0

divisive.conjoint$lgbt.reputation.consistent <- NA
divisive.conjoint$lgbt.reputation.consistent[which(divisive.conjoint$candidate.party=='Republican' & divisive.conjoint$lgbt.candidate==1)] <- 0
divisive.conjoint$lgbt.reputation.consistent[which(divisive.conjoint$candidate.party=='Democrat' & divisive.conjoint$lgbt.candidate==0)] <- 0
divisive.conjoint$lgbt.reputation.consistent[which(divisive.conjoint$candidate.party=='Republican' & divisive.conjoint$lgbt.candidate==0)] <- 1
divisive.conjoint$lgbt.reputation.consistent[which(divisive.conjoint$candidate.party=='Democrat' & divisive.conjoint$lgbt.candidate==1)] <- 1

divisive.conjoint$birthcontrol.reputation.consistent <- NA
divisive.conjoint$birthcontrol.reputation.consistent[which(divisive.conjoint$candidate.party=='Republican' & divisive.conjoint$birthcontrol.candidate==1)] <- 0
divisive.conjoint$birthcontrol.reputation.consistent[which(divisive.conjoint$candidate.party=='Democrat' & divisive.conjoint$birthcontrol.candidate==0)] <- 0
divisive.conjoint$birthcontrol.reputation.consistent[which(divisive.conjoint$candidate.party=='Republican' & divisive.conjoint$birthcontrol.candidate==0)] <- 1
divisive.conjoint$birthcontrol.reputation.consistent[which(divisive.conjoint$candidate.party=='Democrat' & divisive.conjoint$birthcontrol.candidate==1)] <- 1

divisive.conjoint$china.reputation.consistent <- NA
divisive.conjoint$china.reputation.consistent[which(divisive.conjoint$candidate.party=='Republican' & divisive.conjoint$china.candidate==1)] <- 1
divisive.conjoint$china.reputation.consistent[which(divisive.conjoint$candidate.party=='Democrat' & divisive.conjoint$china.candidate==0)] <- 1
divisive.conjoint$china.reputation.consistent[which(divisive.conjoint$candidate.party=='Republican' & divisive.conjoint$china.candidate==0)] <- 0
divisive.conjoint$china.reputation.consistent[which(divisive.conjoint$candidate.party=='Democrat' & divisive.conjoint$china.candidate==1)] <- 0

divisive.conjoint$interior.reputation.consistent <- NA
divisive.conjoint$interior.reputation.consistent[which(divisive.conjoint$candidate.party=='Republican' & divisive.conjoint$interior.candidate==1)] <- 0
divisive.conjoint$interior.reputation.consistent[which(divisive.conjoint$candidate.party=='Democrat' & divisive.conjoint$interior.candidate==0)] <- 0
divisive.conjoint$interior.reputation.consistent[which(divisive.conjoint$candidate.party=='Republican' & divisive.conjoint$interior.candidate==0)] <- 1
divisive.conjoint$interior.reputation.consistent[which(divisive.conjoint$candidate.party=='Democrat' & divisive.conjoint$interior.candidate==1)] <- 1

divisive.conjoint$commoncore.reputation.consistent <- NA
divisive.conjoint$commoncore.reputation.consistent[which(divisive.conjoint$candidate.party=='Republican' & divisive.conjoint$commoncore.candidate==1)] <- 0
divisive.conjoint$commoncore.reputation.consistent[which(divisive.conjoint$candidate.party=='Democrat' & divisive.conjoint$commoncore.candidate==0)] <- 0
divisive.conjoint$commoncore.reputation.consistent[which(divisive.conjoint$candidate.party=='Republican' & divisive.conjoint$commoncore.candidate==0)] <- 1
divisive.conjoint$commoncore.reputation.consistent[which(divisive.conjoint$candidate.party=='Democrat' & divisive.conjoint$commoncore.candidate==1)] <- 1

divisive.conjoint$reputation.consistent.overall <- divisive.conjoint$abortion.reputation.consistent + divisive.conjoint$rifle.reputation.consistent + divisive.conjoint$obamacare.reputation.consistent + divisive.conjoint$lgbt.reputation.consistent + divisive.conjoint$birthcontrol.reputation.consistent + divisive.conjoint$china.reputation.consistent + divisive.conjoint$interior.reputation.consistent + divisive.conjoint$commoncore.reputation.consistent
divisive.conjoint$reputation.consistent.high.salience <- divisive.conjoint$abortion.reputation.consistent + divisive.conjoint$rifle.reputation.consistent + divisive.conjoint$obamacare.reputation.consistent + divisive.conjoint$lgbt.reputation.consistent 
divisive.conjoint$reputation.consistent.low.salience <- divisive.conjoint$birthcontrol.reputation.consistent + divisive.conjoint$china.reputation.consistent + divisive.conjoint$interior.reputation.consistent + divisive.conjoint$commoncore.reputation.consistent


#Covariates to be added to each of an individual's conjoints
covariates <- covariates[c('rnid','divisive.rounds.round1porder','Q2','Q3','Q4','Q5','Q6','Q7','Q9','Q10','Q11','Q53','Q54','Q55','Q56','Q57','Q58','Q59','Q60','Q62','Q63','Q64','Q65','Q66','Q67','Q68','Q69','ResponseId')]
names(covariates) <- c('rnid','divisive.treatment','gender','race','pid','age','income','education','lean.pid','rep.pid.strength','dem.pid.strength','abortion.stance','rifle.stance','obamacare.stance','lgbt.stance','birthcontrol.stance','china.stance','interior.stance','commoncore.stance','abortion.importance','rifle.importance','obamacare.importance','lgbt.importance','birthcontrol.importance','china.importance','interior.importance','commoncore.importance','ResponseId')
covariates$divisive.treatment <- as.numeric(as.character(covariates$divisive.treatment))
covariates <- subset(covariates,covariates$divisive.treatment!='')


#PID Coding

#EP Note share of partisan leaners seems low, was this party of study design?...

covariates$pid.strength <- as.character(covariates$lean.pid)
covariates$pid.strength[which(covariates$pid.strength=="")] <- as.character(covariates$rep.pid.strength[which(covariates$pid.strength=="")])
covariates$pid.strength[which(covariates$pid.strength=="")] <- as.character(covariates$dem.pid.strength[which(covariates$pid.strength=="")])
covariates$pid.strength <- mapvalues(covariates$pid.strength,from=c('Strong Democrat','Strong Republican','Not a strong Democrat','Not a strong Republican','Republican Party','Democratic Party','Neither'),to=c('Strong','Strong','Not Strong','Not Strong','Lean','Lean',NA))

covariates$pid <- as.character(covariates$pid)
covariates$pid[which(covariates$pid%in%c('Other','Independent'))] <- as.character(covariates$lean.pid[which(covariates$pid%in%c('Other','Independent'))])
covariates$pid[which(covariates$pid=='Democratic Party')] <- 'Democrat'
covariates$pid[which(covariates$pid=='Republican Party')] <- 'Republican'
covariates$pid[which(covariates$pid=='Neither')] <- NA


#Issue Positions
covariates$abortion.stance <- ifelse(covariates$abortion.stance=='support',1,0)
covariates$rifle.stance <- ifelse(covariates$rifle.stance=='support',1,0)
covariates$obamacare.stance <- ifelse(covariates$obamacare.stance=='support',1,0)
covariates$lgbt.stance <- ifelse(covariates$lgbt.stance=='support',1,0)
covariates$birthcontrol.stance <- ifelse(covariates$birthcontrol.stance=='support',1,0)
covariates$china.stance <- ifelse(covariates$china.stance=='support',1,0)
covariates$interior.stance <- ifelse(covariates$interior.stance=='support',1,0)
covariates$commoncore.stance <- ifelse(covariates$commoncore.stance=='support',1,0)


out <- na.omit(ddply(covariates,.(pid),summarise,abortion=mean(abortion.stance,na.rm=TRUE),rifle=mean(rifle.stance,na.rm=TRUE),obama=mean(obamacare.stance,na.rm=TRUE),lgbt=mean(lgbt.stance,na.rm=TRUE),interior=mean(interior.stance,na.rm=TRUE),china=mean(china.stance,na.rm=TRUE),common=mean(commoncore.stance,na.rm=TRUE),birth=mean(birthcontrol.stance,na.rm=TRUE)))

xtable(out)

#Issue Salience
covariates$abortion.importance <- as.numeric(as.character(mapvalues(x=covariates$abortion.importance,from=c('Extremely important','Very important','Moderately important','Not that important','No opinion'),to=c(4,3,2,1,1))))
covariates$rifle.importance <- as.numeric(as.character(mapvalues(x=covariates$rifle.importance,from=c('Extremely important','Very important','Moderately important','Not that important','No opinion'),to=c(4,3,2,1,1))))
covariates$obamacare.importance <- as.numeric(as.character(mapvalues(x=covariates$obamacare.importance,from=c('Extremely important','Very important','Moderately important','Not that important','No opinion'),to=c(4,3,2,1,1))))
covariates$lgbt.importance <- as.numeric(as.character(mapvalues(x=covariates$lgbt.importance,from=c('Extremely important','Very important','Moderately important','Not that important','No opinion'),to=c(4,3,2,1,1))))
covariates$birthcontrol.importance <- as.numeric(as.character(mapvalues(x=covariates$birthcontrol.importance,from=c('Extremely important','Very important','Moderately important','Not that important','No opinion'),to=c(4,3,2,1,1))))
covariates$china.importance <- as.numeric(as.character(mapvalues(x=covariates$china.importance,from=c('Extremely important','Very important','Moderately important','Not that important','No opinion'),to=c(4,3,2,1,1))))
covariates$interior.importance <- as.numeric(as.character(mapvalues(x=covariates$interior.importance,from=c('Extremely important','Very important','Moderately important','Not that important','No opinion'),to=c(4,3,2,1,1))))
covariates$commoncore.importance <- as.numeric(as.character(mapvalues(x=covariates$commoncore.importance,from=c('Extremely important','Very important','Moderately important','Not that important','No opinion'),to=c(4,3,2,1,1))))

importance.frame <- apply(X=covariates[,which(names(covariates) %in% c('abortion.importance','rifle.importance','obamacare.importance','lgbt.importance','birthcontrol.importance','china.importance','interior.importance','commoncore.importance'))],MARGIN=2,FUN=mean,na.rm=TRUE)
importance.frame <- cbind.data.frame(importance.frame)
importance.frame$issue <- rownames(importance.frame)
importance.frame <- importance.frame[,c('issue','importance.frame')]
importance.frame <- importance.frame[order(importance.frame$importance.frame,decreasing=TRUE),]
rownames(importance.frame) <- NULL
xtable(importance.frame)

mean(covariates$abortion.importance + covariates$lgbt.importance + covariates$rifle.importance + covariates$obamacare.importance)/4
mean(covariates$birthcontrol.importance + covariates$commoncore.importance + covariates$interior.importance + covariates$china.importance)/4

#
divisive.conjoint <- merge(divisive.conjoint,covariates,by=c('rnid'),all.x=TRUE)
divisive.conjoint$copartisan <- ifelse(divisive.conjoint$candidate.party==divisive.conjoint$pid, 1, 0)

#Code up agreement on case by case basis
divisive.conjoint$abortion.agree <- ifelse(divisive.conjoint$abortion.stance==divisive.conjoint$abortion.candidate, 1, 0)
divisive.conjoint$rifle.agree <- ifelse(divisive.conjoint$rifle.stance==divisive.conjoint$rifle.candidate, 1, 0)
divisive.conjoint$obamacare.agree <- ifelse(divisive.conjoint$obamacare.stance==divisive.conjoint$obamacare.candidate, 1, 0)
divisive.conjoint$lgbt.agree <- ifelse(divisive.conjoint$lgbt.stance==divisive.conjoint$lgbt.candidate, 1, 0)
divisive.conjoint$birthcontrol.agree <- ifelse(divisive.conjoint$birthcontrol.stance==divisive.conjoint$birthcontrol.candidate, 1, 0)
divisive.conjoint$china.agree <- ifelse(divisive.conjoint$china.stance==divisive.conjoint$china.candidate, 1, 0)
divisive.conjoint$interior.agree <- ifelse(divisive.conjoint$interior.stance==divisive.conjoint$interior.candidate, 1, 0)
divisive.conjoint$commoncore.agree <- ifelse(divisive.conjoint$commoncore.stance==divisive.conjoint$commoncore.candidate, 1, 0)

#Code up Total Num of Disagreements
divisive.conjoint$high.salience.agree <- divisive.conjoint$abortion.agree + divisive.conjoint$rifle.agree + divisive.conjoint$obamacare.agree + divisive.conjoint$lgbt.agree
divisive.conjoint$high.salience.disagree <- 4 - divisive.conjoint$high.salience.agree

divisive.conjoint$low.salience.agree <- divisive.conjoint$birthcontrol.agree + divisive.conjoint$china.agree + divisive.conjoint$interior.agree + divisive.conjoint$commoncore.agree
divisive.conjoint$low.salience.disagree <- 4 - divisive.conjoint$low.salience.agree

divisive.conjoint$overall.agree <- divisive.conjoint$high.salience.agree + divisive.conjoint$low.salience.agree
divisive.conjoint$overall.disagree <- 8 - divisive.conjoint$overall.agree

#Analyzing Defection Rates by PID Strength
divisive.conjoint$partisan.defection <- ifelse(divisive.conjoint$copartisan==0 & divisive.conjoint$selected==1, 1, 0)
1 - length(unique(divisive.conjoint$rnid[which(divisive.conjoint$partisan.defection==1)]))/length(unique(divisive.conjoint$rnid))
defection.frame <- ddply(divisive.conjoint,.(rnid),summarise,defections=sum(partisan.defection))
defection.frame <- merge(defection.frame,covariates,by=c('rnid'),all.x=TRUE)
na.omit(ddply(defection.frame,.(pid.strength),summarise,partisan.defections=mean(defections,na.rm=TRUE)))

#Effects of various candidate traits on Pr(Selection)
fit.w.robust <- function(model, cluster.var, dta){	
	robust.se <- function(model, cluster){
		require(sandwich)
		require(lmtest)
		M <- length(unique(cluster))
		N <- length(cluster)
		K <- model$rank
		dfc <- (M/(M - 1)) * ((N - 1)/(N - K))
		uj <- apply(estfun(model), 2, function(x) tapply(x, cluster, sum));
		rcse.cov <- dfc * sandwich(model, meat = crossprod(uj)/N)
		rcse.se <- coeftest(model, rcse.cov)
		return(list(rcse.cov, rcse.se))
	}
	
	out <- model$model
	clustervar <- mapply(paste,cluster.var,dta[!(1:dim(dta)[1] %in% model$na.action),cluster.var],sep="")
	vcov <- robust.se(model, clustervar)
	model$se <- vcov[[1]]
	model$coeftest <- vcov[[2]]
	return(model)
}

load("updated.weight.frame.2.RData")
divisive.conjoint$rnid <- factor(divisive.conjoint$rnid)
dim(divisive.conjoint)
divisive.conjoint <- merge(divisive.conjoint,updated.weight.frame.2,by.x=c('ResponseId'),by.y=c('caseid'),all.x=TRUE)
dim(divisive.conjoint)



#Overall Model Issue Agreement
allissue.model <- lm(selected~ rifle.agree + abortion.agree + lgbt.agree + obamacare.agree + birthcontrol.agree + china.agree + commoncore.agree + interior.agree,data=divisive.conjoint)
allissue.model <- fit.w.robust(allissue.model,'rnid',divisive.conjoint)
apsrtable(allissue.model,coef.names=c('(Intercept)','Rifle','Abortion','LBGT Discrimination','Obamacare','Birth Control','Tariff','Common Core','Dept. of Interior Spending'),caption="Effect of Agreement on Issue on Pr(Select Candidate)")

#Pooled Overall No. Issue Agreement and Party
all.model.number <- lm(selected~copartisan + factor(high.salience.agree) + factor(low.salience.agree),data=divisive.conjoint)
all.model.number <- fit.w.robust(all.model.number,'rnid',divisive.conjoint)

all.model.number.weighted <- lm(selected~copartisan + factor(high.salience.agree) + factor(low.salience.agree),data=divisive.conjoint,weights=divisive.conjoint$weights.nes)
all.model.number.weighted <- fit.w.robust(all.model.number.weighted,'respondent',divisive.conjoint)

all.model.number.dem <- lm(selected~copartisan + factor(high.salience.agree) + factor(low.salience.agree),data=divisive.conjoint[which(divisive.conjoint$pid=='Democrat'),])
all.model.number.dem <- fit.w.robust(all.model.number.dem,'rnid',divisive.conjoint[which(divisive.conjoint$pid=='Democrat'),])

all.model.number.rep <- lm(selected~copartisan + factor(high.salience.agree) + factor(low.salience.agree),data=divisive.conjoint[which(divisive.conjoint$pid=='Republican'),])
all.model.number.rep <- fit.w.robust(all.model.number.rep,'rnid',divisive.conjoint[which(divisive.conjoint$pid=='Republican'),])

apsrtable(all.model.number,all.model.number.weighted,all.model.number.dem,all.model.number.rep,model.names=c('All','All - Reweighted','Democrats Only','Republican Only'),coef.names=c('(Intercept)','Co-Partisan','High-Salience Agree - 1 Issue','High-Salience Agree - 2 Issues','High-Salience Agree - 3 Issues','High-Salience Agree - 4 Issues','Low-Salience Agree - 1 Issue','Low-Salience Agree - 2 Issues','Low-Salience Agree - 3 Issues','Low-Salience Agree - 4 Issues'),caption=c('Effect of Co-Partisanship and Issue Agreement on Candidate Choice - Study 2'))

pdf(file='individualdisagreement-coefficients-study2.pdf')
par(mar=c(4.1,5.75,2,7))
plot(y=9:1,x=all.model.number$coefficients[2:10],pch=16,col='black',yaxt='n',ylab='',xlim=c(0,.6),cex=2,xlab='Effect on Pr(Prefer Candidate)',ylim=c(0.75,9.25))
segments(y0=9:1,x0=all.model.number$coefficients[2:10]-2*all.model.number$coeftest[2:10,2],x1=all.model.number$coefficients[2:10]+2*all.model.number$coeftest[2:10,2],lwd=4)
abline(h=4.5,lwd=2)
abline(h=8.5,lwd=2)
axis(side=2,at=9:1,labels=c('Co-Partisan','1 Issue','2 Issues','3 Issues','4 Issues','1 Issue','2 Issues','3 Issues','4 Issues'),las=1)
axis(side=4,at=c(2.5,6.5,9),labels=c('Minimally Divisive
Issue Agreement','Highly Divisive
Issue Agreement','Partisanship'),las=1,tick=FALSE,line=-.75)
dev.off()

#Reputation Consistent Positioning

copartisan.reputation <- lm(selected~factor(reputation.consistent.high.salience) + factor(reputation.consistent.low.salience),data=divisive.conjoint[which(divisive.conjoint$copartisan==1),])
copartisan.reputation <- fit.w.robust(copartisan.reputation,'rnid',divisive.conjoint[which(divisive.conjoint$copartisan==1),])

outpartisan.reputation <- lm(selected~factor(reputation.consistent.high.salience) + factor(reputation.consistent.low.salience),data=divisive.conjoint[which(divisive.conjoint$copartisan==0),])
outpartisan.reputation <- fit.w.robust(outpartisan.reputation,'rnid',divisive.conjoint[which(divisive.conjoint$copartisan==0),])

pdf(file='reputationdisagreement-coefficients-study2.pdf',height=4,width=8)

par(mar=c(4.1,4.5,2,5.75),mfrow=c(1,2))

plot(y=8:1,x=outpartisan.reputation$coefficients[2:9],pch=16,col='black',yaxt='n',ylab='',xlim=c(-.5,.05),cex=2,xlab='Effect on Pr(Prefer Candidate)',ylim=c(0.75,8.25),main='Out-Partisan Candidate',xaxt='n')
segments(y0=8:1,x0=outpartisan.reputation$coefficients[2:9]-2*outpartisan.reputation$coeftest[2:9,2],x1=outpartisan.reputation$coefficients[2:9]+2*outpartisan.reputation$coeftest[2:9,2],lwd=4)
abline(v=0,lty=2)
abline(h=4.5,lwd=2)
axis(side=2,at=8:1,labels=c('1 Issue','2 Issues','3 Issues','4 Issues','1 Issue','2 Issues','3 Issues','4 Issues'),las=1)
axis(side=4,at=c(2.5,6.5),labels=c('Low-Salience
Reputation
Consistent','High-Salience
Reputation
Consistent'),las=1,tick=FALSE,line=-.75)
axis(side=1,at=c(-0.4,-0.2,0))


plot(y=8:1,x=copartisan.reputation$coefficients[2:9],pch=16,col='black',yaxt='n',ylab='',xlim=c(-.05,.5),cex=2,xlab='Effect on Pr(Prefer Candidate)',ylim=c(0.75,8.25),main='Co-Partisan Candidate',xaxt='n')
segments(y0=8:1,x0=copartisan.reputation$coefficients[2:9]-2*copartisan.reputation$coeftest[2:9,2],x1=copartisan.reputation$coefficients[2:9]+2*copartisan.reputation$coeftest[2:9,2],lwd=4)
abline(v=0,lty=2)
abline(h=4.5,lwd=2)
axis(side=2,at=8:1,labels=c('1 Issue','2 Issues','3 Issues','4 Issues','1 Issue','2 Issues','3 Issues','4 Issues'),las=1)
axis(side=4,at=c(2.5,6.5),labels=c('Low-Salience
Reputation
Consistent','High-Salience
Reputation
Consistent'),las=1,tick=FALSE,line=-.75)
axis(side=1,at=c(0,0.2,0.4))

dev.off()


####
####Levels of Support By Issue Disagreement
####
copartisan.levels <- lm(selected~factor(high.salience.disagree) + factor(low.salience.disagree), data=divisive.conjoint[which(divisive.conjoint$copartisan==1),])
copartisan.levels <-  fit.w.robust(copartisan.levels,'respondent',divisive.conjoint[which(divisive.conjoint$copartisan==1),])
copartisan.levels


outpartisan.levels <- lm(selected~factor(high.salience.disagree) + factor(low.salience.disagree), data=divisive.conjoint[which(divisive.conjoint$copartisan==0),])
outpartisan.levels <- fit.w.robust(outpartisan.levels,'respondent',divisive.conjoint[which(divisive.conjoint$copartisan==0),])

interact.coefs.coparty.high.salience <- c(copartisan.levels$coefficients[1],copartisan.levels$coefficients[1] + copartisan.levels$coefficients[2:5])
interact.coefs.outparty.high.salience <- c(outpartisan.levels$coefficients[1],outpartisan.levels$coefficients[1] + outpartisan.levels$coefficients[2:5])
interact.ses.coparty.high.salience <- c(sqrt(copartisan.levels$se[1,1]),sapply(X=2:5, FUN=function(x) sqrt(copartisan.levels$se[1,1] + copartisan.levels$se[x,x] + 2*copartisan.levels$se[1,x])))
interact.ses.outparty.high.salience <- c(sqrt(outpartisan.levels$se[1,1]),sapply(X=2:5, FUN=function(x) sqrt(outpartisan.levels$se[1,1] + outpartisan.levels$se[x,x] + 2*outpartisan.levels$se[1,x])))

interact.coefs.coparty.low.salience <- c(copartisan.levels$coefficients[1],copartisan.levels$coefficients[1] + copartisan.levels$coefficients[6:9])
interact.coefs.outparty.low.salience <- c(outpartisan.levels$coefficients[1],outpartisan.levels$coefficients[1] + outpartisan.levels$coefficients[6:9])
interact.ses.coparty.low.salience <- c(sqrt(copartisan.levels$se[1,1]),sapply(X=6:9, FUN=function(x) sqrt(copartisan.levels$se[1,1] + copartisan.levels$se[x,x] + 2*copartisan.levels$se[1,x])))
interact.ses.outparty.low.salience <- c(sqrt(outpartisan.levels$se[1,1]),sapply(X=6:9, FUN=function(x) sqrt(outpartisan.levels$se[1,1] + outpartisan.levels$se[x,x] + 2*outpartisan.levels$se[1,x])))


pdf(file="individualdisagreement-levels-study2-copartisan.pdf")
par(mar=c(4.1,4,2,2))

plot(x=0:4-.1,y=interact.coefs.coparty.high.salience,pch=16,las=1,ylim=c(0,1),xlab="# Issue Disagreements With Co-Partisan Candidate",ylab="Pr(Select Candidate)",xlim=c(-.2,4.2),main="",cex=2.5,type='n')
abline(h=.88,lty=2,lwd=2)#ANES Benchmark
abline(h=.5,lwd=2.5)
lines(x=0:4-.1,y=interact.coefs.coparty.high.salience,col='black',lwd=3)
lines(x=0:4+.1,y=interact.coefs.coparty.low.salience,col='gray50',lwd=3)

points(x=0:4-.1,y=interact.coefs.coparty.high.salience,pch=16,cex=2.5, col='black')
points(x=0:4+.1,y=interact.coefs.coparty.low.salience,pch=16,cex=2.5, col='gray50')

legend('topright',legend=c('Highly Divisive','Minimally Divisive'),col=c('black','gray50'),pch=c(16,16),pt.cex=c(2,2))

segments(x0=0:4-.1,y0=interact.coefs.coparty.high.salience+2*interact.ses.coparty.high.salience,y1=interact.coefs.coparty.high.salience-2*interact.ses.coparty.high.salience,lwd=4, col='black')
segments(x0=0:4+.1,y0=interact.coefs.coparty.low.salience+2*interact.ses.coparty.low.salience,y1=interact.coefs.coparty.low.salience-2*interact.ses.coparty.low.salience,lwd=4, col='gray50')

dev.off()

pdf(file="individualdisagreement-levels-study2-outpartisan.pdf")
par(mar=c(4.1,4,2,2))

plot(x=0:4-.1,y=interact.coefs.outparty.high.salience,pch=16,las=1,ylim=c(0,1),xlab="# Issue Disagreements With Out-Party Candidate",ylab="Pr(Select Candidate)",xlim=c(-.2,4.2),main="",cex=2.5,type='n')
abline(h=.12,lty=2,lwd=2,col='gray50')#ANES Benchmark
abline(h=.5,lwd=2)
lines(x=0:4-.1,y=interact.coefs.outparty.high.salience,col='black',lwd=3)
lines(x=0:4+.1,y=interact.coefs.outparty.low.salience,col='gray50',lwd=3)

points(x=0:4-.1,y=interact.coefs.outparty.high.salience,pch=16,cex=2.5, col='black')
points(x=0:4+.1,y=interact.coefs.outparty.low.salience,pch=16,cex=2.5, col='gray50')

legend('topright',legend=c('High-Salience','Low-Salience'),col=c('black','gray50'),pch=c(16,16),pt.cex=c(2,2))

segments(x0=0:4-.1,y0=interact.coefs.outparty.high.salience+2*interact.ses.outparty.high.salience,y1=interact.coefs.outparty.high.salience-2*interact.ses.outparty.high.salience,lwd=4, col='black')
segments(x0=0:4+.1,y0=interact.coefs.outparty.low.salience+2*interact.ses.outparty.low.salience,y1=interact.coefs.outparty.low.salience-2*interact.ses.outparty.low.salience,lwd=4, col='gray50')
dev.off()







pdf(file="individualdisagreement-levels-study2.pdf",height=4,width=8)
#High Salience


# par(mar=c(4,4,2,1),mfrow=c(1,2))
plot(x=0:4-.1,y=interact.coefs.coparty.high.salience,pch=16,las=1,ylim=c(0,1),xlab="# Issue Disagreements With Candidate",ylab="Pr(Select Candidate)",xlim=c(-.2,4.2),main="Co-Partisan",cex=2.5,type='n')
abline(h=.5,lty=3,lwd=2)

lines(x=0:4-.1,y=interact.coefs.coparty.high.salience,col='red',lwd=2)
lines(x=0:4-.1,y=interact.coefs.coparty.low.salience,col='blue',lwd=2)


points(x=0:4-.1,y=interact.coefs.coparty.high.salience,pch=16,cex=1.5, col='red')
points(x=0:4-.1,y=interact.coefs.coparty.low.salience,pch=16,cex=1.5, col='blue')


segments(x0=0:4-.1,y0=interact.coefs.coparty.high.salience+2*interact.ses.coparty.high.salience,y1=interact.coefs.coparty.high.salience-2*interact.ses.coparty.high.salience,lwd=2, col=rgb(red = 1, green = 0, blue = 0, alpha = 0.5))
segments(x0=0:4-.1,y0=interact.coefs.coparty.low.salience+2*interact.ses.coparty.low.salience,y1=interact.coefs.coparty.low.salience-2*interact.ses.coparty.low.salience,lwd=2, col=rgb(red = 0, green = 0, blue = 1, alpha = 0.5))


legend('topright',legend=c('High-Salience','Low-Salience'),col=c('red','blue'),pch=c(16,16),pt.cex=c(1,1),cex=.65)

# #Low Salience
# plot(x=0:4-.1,y=interact.coefs.coparty.low.salience,pch=16,las=1,ylim=c(0,1),xlab="# Issue Disagreements With Candidate",ylab="Pr(Select Candidate)",xlim=c(-.2,4.2),main="Out-Partisan",cex=2.5,type='n')
# abline(h=.5,lty=3,lwd=2)
# 
# lines(x=0:4-.1,y=interact.coefs.outparty.high.salience,col='red',lwd=2)
# lines(x=0:4-.1,y=interact.coefs.outparty.low.salience,col='blue',lwd=2)
# 
# 
# points(x=0:4-.1,y=interact.coefs.outparty.high.salience,pch=16,cex=1.5, col='red')
# points(x=0:4-.1,y=interact.coefs.outparty.low.salience,pch=16,cex=1.5, col='blue')
# 
# 
# segments(x0=0:4-.1,y0=interact.coefs.outparty.high.salience+2*interact.ses.outparty.high.salience,y1=interact.coefs.outparty.high.salience-2*interact.ses.outparty.high.salience,lwd=2, col=rgb(red = 1, green = 0, blue = 0, alpha = 0.5))
# segments(x0=0:4-.1,y0=interact.coefs.outparty.low.salience+2*interact.ses.outparty.low.salience,y1=interact.coefs.outparty.low.salience-2*interact.ses.outparty.low.salience,lwd=2, col=rgb(red = 0, green = 0, blue = 1, alpha = 0.5))
dev.off()



#Difference between High and Low Salience Issue Agree
copartisan.mod <- lm(selected~copartisan + I(high.salience.disagree + low.salience.disagree) + I(high.salience.disagree -low.salience.disagree),data=divisive.conjoint)
copartisan.mod <- fit.w.robust(copartisan.mod,'rnid',divisive.conjoint)
copartisan.mod$coeftest



divisive.conjoint$high.salience.agree.continuous <- as.numeric(as.character(divisive.conjoint$high.salience.agree))
divisive.conjoint$low.salience.agree.continuous <- as.numeric(as.character(divisive.conjoint$low.salience.agree))


all.model.number.continuous <- lm(selected~copartisan + high.salience.agree.continuous + low.salience.agree.continuous,data=divisive.conjoint)
all.model.number.continuous <- fit.w.robust(all.model.number.continuous,'rnid',divisive.conjoint)
summary(all.model.number.continuous)






#############
#Pooled Overall Model Testing Relevant/Irrelevant Difference
copartisan.mod <- lm(selected~copartisan + I(high.salience.disagree + low.salience.disagree) + I(high.salience.disagree -low.salience.disagree),data=divisive.conjoint)
copartisan.mod <- fit.w.robust(copartisan.mod,'rnid',divisive.conjoint)
copartisan.mod$coeftest




#Shifts in Support by Number of Positions

copartisan.mod <- lm(selected~factor(overall.disagree), data=divisive.conjoint,subset=which(copartisan==1))
outpartisan.mod <- lm(selected~factor(overall.disagree), data=divisive.conjoint,subset=which(copartisan==0))
interact.coefs.coparty <- c(copartisan.mod$coefficients[1],copartisan.mod$coefficients[1] + copartisan.mod$coefficients[2:9])
interact.coefs.outparty <-  c(outpartisan.mod$coefficients[1],outpartisan.mod$coefficients[1] + outpartisan.mod$coefficients[2:9])

#Candidate Choice by Personal Issue Disagreements
pdf(file="individualdisagreement-study2.pdf")
plot(x=0:8-.1,y=interact.coefs.coparty,pch=16,las=1,ylim=c(0,1),xlab="# Issue Disagreements With Candidate",ylab="Pr(Select Candidate)",xlim=c(-.2,8.2),main="Candidate Choice by Personal Issue Disagreement",cex=2.5,type='n')
abline(h=.5,lty=1,lwd=2)

lines(x=0:8-.1,y=interact.coefs.coparty,col='black',lty=2,lwd=2)
lines(x=0:8+.1,y=interact.coefs.outparty,col='gray',lty=2,lwd=2)

points(x=0:8-.1,y=interact.coefs.coparty,pch=16,cex=2.5)
points(x=0:8+.1,y=interact.coefs.outparty,col='gray',pch=16,cex=2.5)

legend('topright',legend=c('Co-Partisan (General)','Out-Partisan (General)'),col=c('black','gray'),pch=c(16,16))
#legend('topright',legend=c('Co-Partisan (General)','Out-Partisan (General)'),col=c('black','gray40'),pch=c(16,16))
dev.off()

#Separate by Issue Salience - High
copartisan.mod.high <- lm(selected~factor(high.salience.disagree), data=divisive.conjoint,subset=which(copartisan==1))
outpartisan.mod.high <- lm(selected~factor(high.salience.disagree), data=divisive.conjoint,subset=which(copartisan==0))
interact.coefs.coparty.high <- c(copartisan.mod.high$coefficients[1],copartisan.mod.high$coefficients[1] + copartisan.mod.high$coefficients[2:5])
interact.coefs.outparty.high <-  c(outpartisan.mod.high$coefficients[1],outpartisan.mod.high$coefficients[1] + outpartisan.mod.high$coefficients[2:5])

pdf(file="individualdisagreement-highsalience-study2.pdf")
plot(x=0:4-.1,y=interact.coefs.coparty.high,pch=16,las=1,ylim=c(0,1),xlab="# High-Salience Issue Disagreements With Candidate",ylab="Pr(Select Candidate)",xlim=c(-.2,4.2),main="Candidate Choice by Personal Issue Disagreement
(High-Salience Only)",cex=2.5,type='n')
abline(h=.5,lty=1,lwd=2)

lines(x=0:4-.1,y=interact.coefs.coparty.high,col='black',lty=2,lwd=2)
lines(x=0:4+.1,y=interact.coefs.outparty.high,col='gray',lty=2,lwd=2)

points(x=0:4-.1,y=interact.coefs.coparty.high,pch=16,cex=2.5)
points(x=0:4+.1,y=interact.coefs.outparty.high,col='gray',pch=16,cex=2.5)

legend('topright',legend=c('Co-Partisan (General)','Out-Partisan (General)'),col=c('black','gray'),pch=c(16,16))
#legend('topright',legend=c('Co-Partisan (General)','Out-Partisan (General)'),col=c('black','gray40'),pch=c(16,16))
dev.off()

#Separate by Issue Salience - Low
copartisan.mod.low <- lm(selected~factor(low.salience.disagree), data=divisive.conjoint,subset=which(copartisan==1))
outpartisan.mod.low <- lm(selected~factor(low.salience.disagree), data=divisive.conjoint,subset=which(copartisan==0))
interact.coefs.coparty.low <- c(copartisan.mod.low$coefficients[1],copartisan.mod.low$coefficients[1] + copartisan.mod.low$coefficients[2:5])
interact.coefs.outparty.low <-  c(outpartisan.mod.low$coefficients[1],outpartisan.mod.low$coefficients[1] + outpartisan.mod.low$coefficients[2:5])

pdf(file="individualdisagreement-lowsalience-study2.pdf")
plot(x=0:4-.1,y=interact.coefs.coparty.low,pch=16,las=1,ylim=c(0,1),xlab="# Low-Salience Issue Disagreements With Candidate",ylab="Pr(Select Candidate)",xlim=c(-.2,4.2),main="Candidate Choice by Personal Issue Disagreement
(Low-Salience Only)",cex=2.5,type='n')
abline(h=.5,lty=1,lwd=2)

lines(x=0:4-.1,y=interact.coefs.coparty.low,col='black',lty=2,lwd=2)
lines(x=0:4+.1,y=interact.coefs.outparty.low,col='gray',lty=2,lwd=2)

points(x=0:4-.1,y=interact.coefs.coparty.low,pch=16,cex=2.5)
points(x=0:4+.1,y=interact.coefs.outparty.low,col='gray',pch=16,cex=2.5)

legend('topright',legend=c('Co-Partisan (General)','Out-Partisan (General)'),col=c('black','gray'),pch=c(16,16))
#legend('topright',legend=c('Co-Partisan (General)','Out-Partisan (General)'),col=c('black','gray40'),pch=c(16,16))
dev.off()


pdf(file="individualdisagreement-combinedtogether-study2.pdf",height=4,width=7)
par(mfrow=c(1,2),mar=c(4,4,3,2))

plot(x=0:4-.1,y=interact.coefs.coparty.high,pch=16,las=1,ylim=c(0,1),xlab="# High-Salience Disagreements",ylab="Pr(Select Candidate)",xlim=c(-.2,4.2),main="High-Salience Only",cex=2.5,type='n')
abline(h=.5,lty=1,lwd=2)

lines(x=0:4-.1,y=interact.coefs.coparty.high,col='black',lty=2,lwd=2)
lines(x=0:4+.1,y=interact.coefs.outparty.high,col='gray',lty=2,lwd=2)

points(x=0:4-.1,y=interact.coefs.coparty.high,pch=16,cex=2.5)
points(x=0:4+.1,y=interact.coefs.outparty.high,col='gray',pch=16,cex=2.5)

plot(x=0:4-.1,y=interact.coefs.coparty.low,pch=16,las=1,ylim=c(0,1),xlab="# Low-Salience Disagreements",ylab="Pr(Select Candidate)",xlim=c(-.2,4.2),main="Low-Salience Only",cex=2.5,type='n')
abline(h=.5,lty=1,lwd=2)

lines(x=0:4-.1,y=interact.coefs.coparty.low,col='black',lty=2,lwd=2)
lines(x=0:4+.1,y=interact.coefs.outparty.low,col='gray',lty=2,lwd=2)

points(x=0:4-.1,y=interact.coefs.coparty.low,pch=16,cex=2.5)
points(x=0:4+.1,y=interact.coefs.outparty.low,col='gray',pch=16,cex=2.5)
#legend('topright',legend=c('Co-Partisan (General)','Out-Partisan (General)'),col=c('black','gray40'),pch=c(16,16))
dev.off()




pdf(file="individualdisagreement-combinedinplot-study2.pdf")
plot(x=0:4-.1,y=interact.coefs.coparty.low,pch=16,las=1,ylim=c(0,1),xlab="# Low-Salience Issue Disagreements With Candidate",ylab="Pr(Select Candidate)",xlim=c(-.2,4.2),main="Candidate Choice by Personal Issue Disagreement",cex=2.5,type='n')
abline(h=.5,lty=1,lwd=2)

lines(x=0:4-.1,y=interact.coefs.coparty.high,col='black',lty=2,lwd=2)
points(x=0:4-.1,y=interact.coefs.coparty.high,pch=16,cex=2.5)

lines(x=0:4+.1,y=interact.coefs.coparty.low,lty=2,lwd=2)
points(x=0:4+.1,y=interact.coefs.coparty.low,pch=18,cex=2.5)

lines(x=0:4-.1,y=interact.coefs.outparty.high,col='gray',lty=2,lwd=2)
points(x=0:4-.1,y=interact.coefs.outparty.high,col='gray',pch=16,cex=2.5)

lines(x=0:4+.1,y=interact.coefs.outparty.low,col='gray',lty=2,lwd=2)
points(x=0:4+.1,y=interact.coefs.outparty.low,col='gray',pch=18,cex=2.5)

legend('topright',legend=c('Co-Partisan (General)','Out-Partisan (General)'),col=c('black','gray'),pch=c(16,16))
#legend('topright',legend=c('Co-Partisan (General)','Out-Partisan (General)'),col=c('black','gray40'),pch=c(16,16))
dev.off()


#Reptuation consistent

copartisan.reputation <- lm(selected~factor(I(8-reputation.consistent.overall)),data=divisive.conjoint,subset=which(copartisan==1))
outpartisan.reputation <- lm(selected~factor(I(8-reputation.consistent.overall)),data=divisive.conjoint,subset=which(copartisan==0))

copartisan.reputation.separate<- lm(selected~I(4-reputation.consistent.low.salience) + I(4-reputation.consistent.high.salience),data=divisive.conjoint,subset=which(copartisan==1))
outpartisan.reputation.separate <- lm(selected~ I(4-reputation.consistent.low.salience) + I(4-reputation.consistent.high.salience),data=divisive.conjoint,subset=which(copartisan==0))

